package com.graducationdesign.springboot.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.graducationdesign.springboot.entities.crawling.NewsInformation;
import com.graducationdesign.springboot.service.NewsInformationService;
import com.graducationdesign.springboot.util.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Mr.zhneg
 * @create 2020-05-22-14:52
 */
@Component
public class NewsInformationTask {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private NewsInformationService itemService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //间隔100s执行一次任务

    @Scheduled(fixedDelay = 5000)
    public void  itemTask() throws Exception{
        //1.声明地址
//        http://www.lnpu.edu.cn/xxxw/10.htm?rpvqnhmpepcojsri
        String url = "http://www.lnpu.edu.cn/xxxw.htm";

//获取页面
            String html = httpUtils.doGetHtml(url );
            //解析，并保存页面，获取商品数据并存储
            parse(html);


        System.out.println("数据抓取完成！");

    }

    /**
     * //解析页面，获取商品数据并存储
     * @param html
     */
    private void parse(String html) throws Exception{
        //1.解析html
        Document doc = Jsoup.parse(html);
        logger.info(String.valueOf(doc));
        //获取新闻
        Elements spuEles = doc.select("table.winstyle27654 tr");
        logger.info(String.valueOf(spuEles));
        for (Element spuEle : spuEles){

            //获取新闻标题
            String newsTopic=spuEle.select("a").attr("title");

            //获取新闻url
            String url=spuEle.select("a").attr("href");

            if(url.equals(null) || newsTopic.equals(null)) continue;

            NewsInformation newsInformation=new NewsInformation();

            newsInformation.setCreated(new Date());
            newsInformation.setNewstopic(newsTopic);
            newsInformation.setUrl(url);

            itemService.save(newsInformation);
            System.out.println("数据保存成功");

        }

    }
}
